/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.controller;

import es.silenus.detecta.Constants;

import es.silenus.detecta.dao.KnowledgeUnitDAO;
import es.silenus.detecta.dao.Page;

import es.silenus.detecta.model.KnowledgeUnit;
import es.silenus.detecta.model.User;

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Knowledge unit controller.
 *
 * @author <a href="mailto:mpacior@silenus-consultoria.es">Manuel Pacior</a>
 *
 * @since 08-abr-2009 12:45:09
 */
public class KnowledgeUnitController extends BaseController {
	/**
	 * The knowledge unit DAO.
	 */
	private KnowledgeUnitDAO knowledgeUnitDAO;

	/**
	 * Sets the knowledge unit DAO.
	 *
	 * @param knowledgeUnitDAO the knowledge unit DAO.
	 */
	public void setKnowledgeUnitDAO(KnowledgeUnitDAO knowledgeUnitDAO) {
		this.knowledgeUnitDAO = knowledgeUnitDAO;
	}

	/**
	 * Finds units.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String findUnits(final HttpServletRequest request, final HttpServletResponse response) {
		User current = getUser(request);

		Map<String, Object> sample = new HashMap<String, Object>();

		sample.put("organization.id", current.getOrganization().getId());

		String temp = request.getParameter("code");

		if(temp != null) {
			sample.put("code", temp);
		}

		temp = request.getParameter("name");

		if(temp != null) {
			temp = StringUtils.replaceChars(temp, CharUtils.CR, ' ');
			temp = StringUtils.replaceChars(temp, CharUtils.LF, ' ');
			sample.put("name", temp);
		}

		temp = request.getParameter("description");

		if(temp != null) {
			temp = StringUtils.replaceChars(temp, CharUtils.CR, ' ');
			temp = StringUtils.replaceChars(temp, CharUtils.LF, ' ');
			sample.put("description", temp);
		}

		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String[] sortFields = request.getParameterValues("sort");
		String[] sortDirections = request.getParameterValues("dir");


		Page<KnowledgeUnit> page = knowledgeUnitDAO.findByExample(sample, start, limit, sortFields, sortDirections);

		request.setAttribute("page", page);

		return "secure/organization/units";
	}

	/**
	 * Updates or creates a unit.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String updateUnit(final HttpServletRequest request, final HttpServletResponse response) {
		User current = getUser(request);

		String id = request.getParameter("id");

		long unitId = (id != null) ? Integer.parseInt(id) : 0;

		KnowledgeUnit knowledgeUnit;

		if(unitId > 0) {
			knowledgeUnit = knowledgeUnitDAO.findById(unitId);
		} else {
			knowledgeUnit = new KnowledgeUnit();
		}

		String temp = request.getParameter("code");

		if(temp != null) {
			knowledgeUnit.setCode(temp);

			KnowledgeUnit otherKnowledgeUnit = knowledgeUnitDAO.findByCode(temp, current.getOrganization().getId());

			if((otherKnowledgeUnit != null) && (otherKnowledgeUnit.getId() != unitId)) {
				request.setAttribute(Constants.ERRORS, "unit.code.duplicated");

				return "secure/organization/unit-update-error";
			}
		}

		temp = request.getParameter("name");
		temp = StringUtils.replaceChars(temp, CharUtils.CR, ' ');
		temp = StringUtils.replaceChars(temp, CharUtils.LF, ' ');
		knowledgeUnit.setName(temp);

		temp = request.getParameter("description");
		temp = StringUtils.replaceChars(temp, CharUtils.CR, ' ');
		temp = StringUtils.replaceChars(temp, CharUtils.LF, ' ');
		knowledgeUnit.setDescription(temp);
		knowledgeUnit.setOrganization(current.getOrganization());

		if(unitId > 0) {
			knowledgeUnitDAO.update(knowledgeUnit);
		} else {
			knowledgeUnitDAO.save(knowledgeUnit);
		}

		request.setAttribute("unit", knowledgeUnit);

		return "secure/organization/unit-updated";
	}

	/**
	 * Checks remove units.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String checkDeleteUnits(final HttpServletRequest request, final HttpServletResponse response) {
		String[] units = request.getParameterValues("elements");

		if(units != null) {
			Long[] unitIds = new Long[units.length];

			for(int i = 0; i < units.length; ++i) {
				unitIds[i] = Long.parseLong(units[i]);
			}

			List theUnits = knowledgeUnitDAO.getRemovalInformation(unitIds);

			request.setAttribute("units", theUnits);
		}

		return "secure/organization/units-check-removed";
	}

	/**
	 * Remove units.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String deleteUnits(final HttpServletRequest request, final HttpServletResponse response) {
		String[] units = request.getParameterValues("elements");

		if(units != null) {
			Long[] unitIds = new Long[units.length];

			for(int i = 0; i < units.length; ++i) {
				unitIds[i] = Long.parseLong(units[i]);
			}

			knowledgeUnitDAO.remove(unitIds);
		}

		return "secure/organization/unit-removed";
	}
}
